<?php

namespace app\admin\controller;

use app\model\ScoreLogModel;
use http\Client\Curl\User;
use support\Request;
use support\Response;
use app\model\ScoreChargeModel;
use app\model\UserModel;
use Exception;

class ScoreChargeController
{
    /**
     * 获取用户充值记录列表
     * 
     * @param Request $request
     * @return Response
     */
    public function index(Request $request): Response
    {
        try {
            $current = (int)$request->get('current', 1);
            $pageSize = (int)$request->get('pageSize', 10);
            $userId = (int)$request->get('user_id', 0);
            
            if ($userId <= 0) {
                throw new Exception('用户ID不能为空');
            }
            
            $query = ScoreChargeModel::where('user_id', $userId)
                ->orderBy('created_at', 'desc');
                
            $total = $query->count();
            $list = $query->offset(($current - 1) * $pageSize)
                ->limit($pageSize)
                ->get();

            $records = [];
            foreach ($list as $item) {
                $records[] = [
                    'id' => $item->id,
                    'user_id' => $item->user_id,
                    'score' => $item->score,
                    'status' => $item->status,
                    'status_text' => $this->getStatusText($item->status),
                    'transaction_id' => $item->transaction_id,
                    'out_trade_no' => $item->out_trade_no,
                    'created_at' => $item->created_at ? date('Y-m-d H:i:s', $item->created_at) : '-',
                    'paid_at' => $item->paid_at ? date('Y-m-d H:i:s', $item->paid_at) : '-',
                ];
            }

            return json([
                'data' => [
                    'list' => $records,
                    'total' => $total
                ],
                'msg' => '获取成功',
                'code' => 20000
            ]);
        } catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }

    /**
     * 获取状态文本
     * 
     * @param string $status
     * @return string
     */
    private function getStatusText($status): string
    {
        switch ($status) {
            case 'unpay':
                return '待支付';
            case 'pay':
                return '已支付';
            case 'fail':
                return '支付失败';
        }
    }

    public function charge(Request $request){
        try {
            $input = $request->post();
            $user = UserModel::find($input['user_id']);
            if($input['score'] > 0){
                $user->score += $input['score'];
            }else{
                $user->score -= abs($input['score']);
            }
            $user->updated_at = time();
            $user->save();

            ScoreLogModel::add($user->id,abs($input['score']),'credit',($input['score'] > 0 ? ScoreLogModel::IN : ScoreLogModel::OUT),'system',[
                'comment'=>$input['comment']
            ]);

            return json([
                'msg' => '获取成功',
                'code' => 20000
            ]);
        }catch (Exception $e) {
            return json([
                'data' => null,
                'msg' => $e->getMessage(),
                'code' => 0
            ]);
        }
    }
} 